﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.IO;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web.Script.Serialization;
using System.Web;
using System.Net;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.IO;
using System.Linq;

namespace WechatSDK
{
    public partial class Add_RP_Customer : System.Web.UI.Page
    {
        public static string TSKflag = "0";
        string tokenValue = "";
      
        public string appId = "";
        public string timestamp = "";
        public string nonceStr = "";
        public static int imgcount = 0;
        public string signature = "";
        string EQID = "wx000000";
        string DeptID = "3";
        protected void Page_Load(object sender, EventArgs e)
        {
            EQID = Request["i"].Trim().ToString();
            
            if (!IsPostBack)
            {
                imgcount = 0;
                TSKflag = Guid.NewGuid().ToString();
            }
            Hashtable ht = JSSDK.getSignPackage();
            appId = ht["appId"].ToString();
            nonceStr = ht["nonceStr"].ToString();
            timestamp = ht["timestamp"].ToString();
            signature = ht["signature"].ToString();

            DBS md = new DBS();




            DataSet ds1 = md.EQInfo_By_ID(EQID);

            EQID = ds1.Tables[0].Rows[0][7].ToString();
            DeptID = ds1.Tables[0].Rows[0][6].ToString();
            eqname.Value = ds1.Tables[0].Rows[0][0].ToString();
           // string code = Request["code"];

            //4.根据企业ID:XX,Secret:XXX,获取token
            string accessTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
            string accessTokenResult = HttpGet(accessTokenUrl, "corpid=wx8579b2dae40753d2&corpsecret=JjVqfS47AEc9Z9UHhDvksIGZ_YPgW6Wjo80jGOTxKX8m8yn4VoHDaxRXJI43dIa5");


            JavaScriptSerializer js = new JavaScriptSerializer();
            token mytoken = js.Deserialize<token>(accessTokenResult);
            tokenValue = mytoken.access_token;
        }

        protected void Button1_Click1(object sender, System.EventArgs e)
        {
            imgcount++;
            System.Collections.Specialized.NameValueCollection nc = new System.Collections.Specialized.NameValueCollection(Request.Form);
           

            string mediaid = nc.GetValues("hide_file")[0].ToString();

            if (imgcount == 1)
            {
                Image1.ImageUrl = mediaid;

                Image2.ImageUrl = "dist/example/images/plus.png";
            }

            if (imgcount == 2)
            {
                Image2.ImageUrl = mediaid;
                Image3.ImageUrl = "dist/example/images/plus.png";
            }
            if (imgcount == 3)
            {
                Image3.ImageUrl = mediaid;
                Image4.ImageUrl = "dist/example/images/plus.png";
            }
            if (imgcount == 4)
            {
                Image4.ImageUrl = mediaid;

            }

            //将serverid存入数据库


            DBS d = new DBS();
            d.Add_Task_File(TSKflag, nc.GetValues("serverid")[0].ToString(), "jpg", tokenValue);
        }

        protected void Button2_Click(object sender, System.EventArgs e)
        {
            Button2.Enabled = false;

            if (phone.Value.ToString().Length > 6 )
            {

                string dsc;
                DBS m = new DBS();
                if (EQID == "wx000000")
                {
                    dsc = "设备名称： " + eqname.Value.ToString() + "   故障描述: " + dscr.Value.ToString();
                }
                else
                    dsc = dscr.Value.ToString();
                dsc = dsc.Replace(" ", "");
                m.Add_Task(TSKflag, EQID, dsc, "无", Text1.Value.ToString(), DateTime.Now.ToLocalTime().ToString(), "无", "478", "等待回应", "0", "0", "0", DeptID, "1", "维修", "0", phone.Value.ToString(), Text1.Value.ToString());



                SendMSG sd = new SendMSG();
                string rs = sd.Send("Leon|zhangxin|yangyan|ysl|gyb|yzy", Text1.Value.ToString() + " 刚刚报修！http://www.cmuam.site/Task_Info_Simple.aspx?Flag=" + TSKflag);
                Response.Redirect("Finish_Close.aspx");
            }
            else
            {
                Button2.Enabled = true;
              

                if (phone.Value.ToString().Length < 6)
                    Response.Write("<script>alert('输入电话号码有误！')</script>");




            }
            
        }

        public class userinfo
        {
            public string UserId { get; set; }
            public string DeviceId { get; set; }
        }

        public class token
        {

            public string access_token { get; set; }
            public int expires_in { get; set; }

        }





        public string PostWebRequest(string postUrl, string paramData, Encoding dataEncode)
        {
            string ret = string.Empty;
            try
            {
                byte[] byteArray = dataEncode.GetBytes(paramData); //转化
                HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(postUrl));
                webReq.Method = "POST";
                webReq.ContentType = "application/x-www-form-urlencoded";

                webReq.ContentLength = byteArray.Length;
                Stream newStream = webReq.GetRequestStream();
                newStream.Write(byteArray, 0, byteArray.Length);//写入参数
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
                ret = sr.ReadToEnd();
                sr.Close();
                response.Close();
                newStream.Close();
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            return ret;
        }
        public string HttpGet(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }

    }
}